package com.xiaoyun.controller;

import com.xiaoyun.log.LogConstants;
import com.xiaoyun.log.LogGenerator;
import com.xiaoyun.service.IFeedbackService;
import com.xiaoyun.service.IGainPassTemplateService;
import com.xiaoyun.service.IInventoryService;
import com.xiaoyun.service.IUserPassService;
import com.xiaoyun.vo.Feedback;
import com.xiaoyun.vo.GainPassTemplateRequest;
import com.xiaoyun.vo.Pass;
import com.xiaoyun.vo.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/passbook")
@Api
public class PassbookController {

    private final IUserPassService userPassService;
    private final IInventoryService inventoryService;
    private final IGainPassTemplateService gainPassTemplateService;
    private final IFeedbackService feedbackService;
    private final HttpServletRequest httpServletRequest;

    @Autowired
    public PassbookController(IUserPassService userPassService, IInventoryService inventoryService, IGainPassTemplateService gainPassTemplateService, IFeedbackService feedbackService, HttpServletRequest httpServletRequest) {
        this.userPassService = userPassService;
        this.inventoryService = inventoryService;
        this.gainPassTemplateService = gainPassTemplateService;
        this.feedbackService = feedbackService;
        this.httpServletRequest = httpServletRequest;
    }

    /**
     * 获取用户个人优惠券信息
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @ApiOperation("获取用户个人优惠券信息")
    @GetMapping("/userpassinfo")
    Response userPassInfo(Long userId) throws Exception {
        LogGenerator.genLog(
                httpServletRequest,
                userId,
                LogConstants.ActionName.USER_PASS_INFO,
                null
        );
        return userPassService.getUserPassInfo(userId);
    }

    /**
     * 获取用户使用了的优惠券信息
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @ApiOperation("获取用户使用了的优惠券信息")
    @GetMapping("/userusedpassinfo")
    Response userUsedPassInfo(Long userId) throws Exception {
        LogGenerator.genLog(
                httpServletRequest,
                userId,
                LogConstants.ActionName.USER_USED_PASS_INFO,
                null
        );
        return userPassService.getUserUsePassInfo(userId);
    }

    /**
     * 用户使用优惠券
     *
     * @param pass
     * @return
     * @throws Exception
     */
    @ApiOperation("用户使用优惠券")
    @PostMapping("/userusepass")
    Response userUsePass(@RequestBody Pass pass) throws Exception {
        LogGenerator.genLog(
                httpServletRequest,
                pass.getUserId(),
                LogConstants.ActionName.USER_USE_PASS,
                null
        );
        return userPassService.userUsePass(pass);
    }

    /**
     * 获取用户库存信息
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @ApiOperation("获取用户库存信息")
    @GetMapping("/invetoryinfo")
    Response inventoryInfo(Long userId) throws Exception {
        LogGenerator.genLog(
                httpServletRequest,
                userId,
                LogConstants.ActionName.INVENTORY_INFO,
                null
        );
        return inventoryService.getInventoryInfo(userId);
    }

    /**
     * 用户领取优惠券
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation("用户领取优惠券")
    @PostMapping("/gainpasstemplate")
    Response gainPassTemplate(@RequestBody GainPassTemplateRequest request) throws Exception {
        LogGenerator.genLog(
                httpServletRequest,
                request.getUserId(),
                LogConstants.ActionName.GAIN_PASS_TEMPLATE,
                request
        );
        return gainPassTemplateService.gainPassTemplate(request);
    }

    /**
     * 用户创建评论
     *
     * @param feedback
     * @return
     */
    @ApiOperation("用户创建评论")
    @PostMapping("/createfeedback")
    Response createFeedback(@RequestBody Feedback feedback) {
        LogGenerator.genLog(
                httpServletRequest,
                feedback.getUserId(),
                LogConstants.ActionName.CREATE_FEEDBACK,
                feedback
        );
        return feedbackService.createFeedback(feedback);
    }

    /**
     * 用户获取评论信息
     *
     * @param userId
     * @return
     */
    @ApiOperation("用户获取评论信息")
    @GetMapping("/getfeedback")
    Response getFeedback(Long userId) {
        LogGenerator.genLog(
                httpServletRequest,
                userId,
                LogConstants.ActionName.GET_FEEDBACK,
                null
        );
        return feedbackService.getFeedback(userId);
    }

    /**
     * 异常演示接口
     *
     * @return
     * @throws Exception
     */
    @ApiOperation("异常演示接口")
    @GetMapping("/exception")
    Response exception() throws Exception {
        throw new Exception("Welcome To Xiaoyun");
    }

}
